/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using OfficeOpenXMLInterop;

namespace Theme_Builder.Utility
{
    /// <summary>
    /// Utility functionality around supporting the output of the minimum required information for an OSS
    /// </summary>
    public class MinOssUtil
    {
        private static CT_OfficeStyleSheet baseTheme = null;

        public MinOssUtil()
        {
        }

        /// <summary>
        /// Creates an office style sheet that contains a minimum amount of useful information
        /// </summary>
        /// <returns>Office Style Sheet</returns>
        public static CT_OfficeStyleSheet GetBaseStyleSheet()
        {
            try
            {
                // If we currently haven't created this base object, create it from the stored resource
                if (baseTheme == null)
                {
                    TextReader OssAsString = new StringReader(global::Theme_Builder.Utility.Properties.Resources.BaseOSS);

                    XmlSerializer serializer = new XmlSerializer(typeof(CT_OfficeStyleSheet));

                    baseTheme = (CT_OfficeStyleSheet)serializer.Deserialize(OssAsString);
                }
            }
            catch
            {
                System.Diagnostics.Debug.Assert(false, "Unable to create base style sheet");
            }

            return baseTheme;
        }

        /// <summary>
        /// Verifies that the input style sheet contains the minimum amount of required xml to load correctly
        /// </summary>
        /// <param name="checkStyleSheet">Office Style Sheet to check</param>
        /// <returns>Office Style Sheet that contains the minimum information (original if there were no changes)</returns>
        public static CT_OfficeStyleSheet VerifyComplete(CT_OfficeStyleSheet checkStyleSheet)
        {
            // Check if we're null, fill in the info if so
            if (baseTheme == null)
                GetBaseStyleSheet();

            // If nothing, return base
            if (checkStyleSheet == null || checkStyleSheet.themeElements == null)
            {
                return baseTheme;
            }

            // TODO: there is still work to do here to finish out the comparisons and fix up
            //  any themes that could not be complete, however, most of the time we should be
            //  good based on the initial settings

            return checkStyleSheet;
        }
    }
}
